package hubur

import (
	crand "crypto/rand"
	"fmt"
	"github.com/denisbrodbeck/machineid"
	uuid "github.com/satori/go.uuid"
	"io"
	"strings"
)

func GetMachineId(appId string) (string, error) {
	uid, err := machineid.ProtectedID(appId)
	if err != nil {
		uid = uuid.NewV4().String()
		uid = strings.ReplaceAll(uid, "-", "")
	}
	return uid, nil
}

// UUIdV4 generate a random UUID of version 4 according to RFC 4122
func UUIdV4() (string, error) {
	uuid := make([]byte, 16)

	n, err := io.ReadFull(crand.Reader, uuid)
	if n != len(uuid) || err != nil {
		return "", err
	}

	uuid[8] = uuid[8]&^0xc0 | 0x80
	uuid[6] = uuid[6]&^0xf0 | 0x40

	return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
}
